package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.Activity;
import org.rcloud.io.model.Activity_copy;
import org.rcloud.io.model.Picture;
import org.rcloud.io.service.ActivityService;
import org.rcloud.io.service.Activity_copyService;
import org.rcloud.io.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by Administrator on 2019/3/3.
 */
@RestController
@RequestMapping(value = "/Activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @Autowired
    private PictureService pictureService;

    @Autowired
    private Activity_copyService activity_copyService;

    @RequestMapping(value = "/add",produces = {"application/json;charset=UTF-8"})
    public ResultVo add(@RequestBody String params){

        JSONObject object = JSONObject.parseObject(params);
        object.put("id", UUID.randomUUID());
        object.put("createtime",new Date());
        object.put("flag",1);
        Activity activity = JSONObject.parseObject(object.toString(),Activity.class);
        return activityService.insertSelective(activity);

    }

     /*
    * 修改
    *
    * */

    @RequestMapping(value = "/update", produces = {"application/json;charset=UTF-8"})
    public ResultVo update(@RequestBody String params) {
        Activity activity = JSONObject.parseObject(params, Activity.class);
        return activityService.updateByPrimaryKey(activity);
    }

    /*
   * 查询主活动的信息以及主活动下每个副活动的信息
   *
   * */
    @RequestMapping(value = "/select", produces = {"application/json;charset=UTF-8"})
    public JSONObject select(@RequestBody String params) {//只传了个主id
        Activity activity = JSONObject.parseObject(params, Activity.class);
        Activity activityMain = (Activity) activityService.selectByPrimaryKey(activity).getInfo();
        JSONObject jsonObject = (JSONObject) JSON.toJSON(activityMain); // JSON.toJSON：对象转JSON;
        Picture pictureParams = new Picture();
        pictureParams.setActivid(jsonObject.get("id").toString());
        List<Picture> picture = pictureService.getByActivid(pictureParams).getInfo();
        jsonObject.put("picture",picture);//json新建个键值对，key值存的是picture集合

        List<Activity_copy> activity_copyList = activity_copyService.selectByActid(jsonObject.get("id").toString()).getInfo();
        List tempList = new ArrayList();
        for(Activity_copy activity_copy : activity_copyList){//从activity_copyList中每取值一个循环一次再存入activity_copy对象中
            Picture pictureParamsTemp = new Picture();
            pictureParamsTemp.setActivcopyid(activity_copy.getId());
            List<Picture> picture_copy = pictureService.selectByActivcopyid(pictureParamsTemp).getInfo();//各副活动中对应的图片集合
            JSONObject jsonActivity_copy = (JSONObject) JSON.toJSON(activity_copy);// 对象(主活动下的副活动)转JSON;
            jsonActivity_copy.put("picture",picture_copy);//json新建个键值对，key值存的是各副活动中对应的图片集合
            tempList.add(jsonActivity_copy);
        }
        jsonObject.put("activity_copy", tempList);//json新建个键值对，key值存的是副活动集合

        return jsonObject;
    }
}
